<template>
    <div class="app-container">
      <el-form label-width="120px" :model="params" :rules="formRules" ref="dataForm">
        <el-form-item label="选修课程" prop="courseId">
          <el-select v-model="params.courseId" filterable clearable placeholder="请选择">
            <el-option
                v-for="cOption in courseOptions"
                :key="cOption.id"
                :label="cOption.name"
                :value="cOption.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学生" prop="studentId">
          <el-select v-model="params.studentId" filterable clearable placeholder="请选择">
            <el-option
                v-for="sOption in studentOption"
                :key="sOption.id"
                :label="sOption.name"
                :value="sOption.id">
            </el-option>
          </el-select>
        </el-form-item>      
        <el-form-item>
          <el-button
            :disabled="saveBtnDisabled"
            type="primary"
            @click="saveOrUpdate">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
</template>
  <script>
  import courseSelection from '@/api/cms/courseSelection'
  import courses from '@/api/cms/courses'
  import students from '@/api/cms/students'
  export default {
    data() {
      return {
        // 存放请求信息
        params: {
          courseId:undefined,
          studentId:undefined
        },
        saveBtnDisabled: false, // 保存按钮是否禁用
        courseOptions: undefined,
        studentOption: undefined,
        formRules: {
            studentId: [
                { required: true, message: '请输入学生', trigger: 'blur' }
            ],
            courseId: [
                { required: true, message: '请输入选修课', trigger: 'blur' }
            ]
        }
      }
    },
    watch: {
      $route(to, from) { // 监听路由变化方式
        this.init()
      }
    },
    created() {
      this.init();
      // 获取option数据
      this.getCourseOption();
      this.getStudentOption();
    },
    methods: {
      init() {
        // 判断是否有id
        if (this.$route.params && this.$route.params.id) {
          const id = this.$route.params.id
          this.getInfo(id)
        } else {
            // 做添加 清空表单
            this.params= {
              courseId:undefined,
              studentId:undefined
            }
        }
      },
      // 获取选修课数据
      getCourseOption() {
        courses.getElective().then(res=>{
          console.log(res)
          this.courseOptions = res.data.content
        }).catch(err=>{
          console.log(err)
        })
      },
      // 获取学生数据
      getStudentOption() {
        let params = {}
        students.getListPage(1,999999,params).then(res=>{
          this.studentOption = res.data.content
        }).catch(err=>{
          console.log(err)
        })
      },
      // 根据id查询方法
      getInfo(id) {
        console.log("********")
        courseSelection.getInfoById(id)
          .then(response => {
            console.log(response);
            this.params = response.data.item
          })
      },
      saveOrUpdate() {
        this.$refs.dataForm.validate(valid => {
            // console.log(valid)
            if (valid) {
                // console.log('通过')
            // 表单验证通过，执行提交操作
            this.saveBtnDisabled = true
            // 判断saveBtnDisabled是否有id 修改或添加
                if (!this.params.id) {
                // 添加
                this.saveData()
                } else {
                this.updateData()
                }
            } else {
            // 表单验证失败，输出错误信息
            // console.log('不通过')
                this.$message.error('请正确输入表单');
            }
        })
      },
      // 修改方法
      updateData() {
        courseSelection.updateInfo(this.params)
          .then(response => {
            this.$message({
              type: 'success',
              message: '修改成功!'
            })
            this.$router.push({ path: '/courseSelection/table' })
          }).catch(response => {
            this.$message({
              type: 'error',
              message: '修改失败'
            })
          })
      },
      // 保存
      saveData() {
        courseSelection.addInfo(this.params)
          .then(response => { // 添加成功
            this.$message({
              type: 'success',
              message: '保存成功!'
            })
            // 路由跳转
            this.$router.push({ path: '/courseSelection/table' })
          }).catch(response => {
            this.$message({
              type: 'error',
              message: '保存失败'
            })
          })
      }
    }
  }
  </script>
  